<template>
  <div>
    <el-card>

      <el-container>
       <el-aside width="300px" >
        <span >巡检人员</span>
        <el-table highlight-current-row height="400px" :data="xunjian.users" @expand-change="change">

          <el-table-column type="expand">
            <template slot-scope="scope">
            <el-table  highlight-current-row height="200px" v-if="scope.row.gantas!=null" :data="scope.row.gantas">
             <el-table-column label="杆塔名" prop="gcode"></el-table-column>
            </el-table>
            </template>
          </el-table-column>
          <el-table-column prop="uname" lable="巡检员"></el-table-column>
          <el-table-column>
            <template slot-scope="scope">
            <el-button type="text" size="small" @click="baocun(scope.row)">保存</el-button>
            </template>
          </el-table-column>
        </el-table>
       </el-aside>
       <el-main>
         <span>可分配的杆塔</span>
         <br>
         <br>

        <el-tree
          
          :data="gantas"
          :props="props"
          show-checkbox
          style="margin-left: 300px;"
          @check="checkchange"
           ref="tree"
           node-key="gid"
         >
        </el-tree>
      <!--  <span v-else>杆塔分配完成</span> -->
       </el-main>
      </el-container>
      <template>
        <el-button @click="xiugaitype">保存</el-button>
         <el-button @click="fanhui">返回</el-button>
      </template>
    </el-card>



  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    data() {
      return {
        xunjian: [],
        user: [],
        props:{
          label:'gcode',
          gid:'gid'
        },
        ganta:[],
        xzz:{
        gantaid:'',
        zhuid:'',
        uid:''
        },
        gantas:[],
        userGanta:[]
      }
    },
    created() {
      this.xunjian = this.$route.query
      this.getGantas()
    },
    methods: {
      add: function(item) {
        console.log(item)
      },
      fanhui:function(){
        this.$router.push({path:'/ZhiDingAndFenPei'})
      },
      checkchange(data, data2, indeterminate){
      this.ganta=data2.checkedNodes
      },
      baocun:function(row){
          this.$confirm('分配之后不可修改, 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {

                  if(this.ganta==null||this.ganta==''){
                          this.$message("请选择要分配的杆塔")
                          return
                        }
                        this.xzz.uid=row.uid
                         this.xzz.zhuid=this.xunjian.id
                      this.ganta.forEach(item=>{
                       this.xzz.gantaid+=item.gid+','
                       console.log(item.gid)
                       let postData=this.$qs.stringify({
                           gid:item.gid
                         })
                      axios.post("http:localhost:8086/ganta/updateRuntype",postData).then(vs=>{
                         })
                         console.log(this.xzz.gantaid)
                       })
                        let postDate=this.$qs.stringify({
                       zhuid:this.xzz.zhuid,
                       gantaid:this.xzz.gantaid,
                       uid:this.xzz.uid
                        })
                        axios.post('http:localhost:8086/zi/add',postDate).then(vs=>{
                          this.$refs.tree.setCheckedKeys([]);
                          this.getGantas()
                          console.log(1)
                         this.xzz.gantaid=''
                         this.ganta=[]
                        })

                        this.$message('分配成功')
                }).catch(() => {
                  this.$message({
                    type: 'info',
                    message: '分配失败'
                  });
                });

                  this.getGantas()


      },
      getGantas:function(){

        console.log(this.xunjian.xianlu.xlnum)
        let postData=this.$qs.stringify({
          xlnum:this.xunjian.xianlu.xlnum
        })
        axios.post("http:localhost:8086/ganta/getNoFenPei",postData).then(vs=>{
          this.gantas=vs.data
        })

      },
      xiugaitype:function(){
        if(this.gantas.size>0){
          this.$message('需要巡检的杆塔还没有分配完,不能进行保存')
          return
        }
      console.log(this.xunjian.id)
      let postData=this.$qs.stringify({
        id:this.xunjian.id
      })
      axios.post("http:localhost:8086/zhu/updatetype",postData).then(vs=>{
         this.$router.push({path:'/ZhiDingAndFenPei'})
      })
      },
      change:function(row){
        console.log(row)
        let postDate=this.$qs.stringify({
          uid:row.uid
        })
        axios.post("http:localhost:8086/zi/selectXunjianGantaByuid",postDate).then(vs=>{
       row.gantas=vs.data
        })
      }
    }


  }
</script>

<style>
</style>
